package com.sansecy.customview.example;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import static android.graphics.Color.GREEN;

public class PaintUsageView extends View {
    private Paint mPaint;
    //顶部线
    private int top = 200;
    private String text = "中文 Chinese English";
    private int baseLineY = 200;

    public PaintUsageView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setTextSize(120);
        mPaint.setTextAlign(Paint.Align.LEFT);
    }

    public PaintUsageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //计算基线
        Paint.FontMetricsInt fm = mPaint.getFontMetricsInt();
        
        //画top线
        mPaint.setColor(Color.BLACK);
        canvas.drawLine(0, baseLineY + fm.top, 3000, baseLineY + fm.top, mPaint);

        //画top安全线
        mPaint.setColor(Color.MAGENTA);
        canvas.drawLine(0, fm.ascent + baseLineY, 3000, fm.ascent + baseLineY, mPaint);

        //画基线
        mPaint.setColor(Color.RED);
        canvas.drawLine(0, baseLineY, 3000, baseLineY, mPaint);

        //画bottom安全线
        mPaint.setColor(Color.MAGENTA);
        canvas.drawLine(0, fm.descent + baseLineY, 3000, fm.descent + baseLineY, mPaint);

        //画bottom线
        mPaint.setColor(Color.RED);
        canvas.drawLine(0, fm.bottom + baseLineY, 3000, fm.bottom + baseLineY, mPaint);

        //写文字
        mPaint.setColor(GREEN);
        canvas.drawText(text, 0, baseLineY, mPaint);
    }

}
